package com.zixuan.sina.controller;

import com.aliyuncs.exceptions.ClientException;
import com.zixuan.sina.config.rabbitmq.ConsumerTest;
import com.zixuan.sina.config.rabbitmq.DirectChange;
import com.zixuan.sina.entity.Post;
import com.zixuan.sina.entity.vo.PostMangerVo;
import com.zixuan.sina.entity.vo.PostQuery;
import com.zixuan.sina.entity.vo.PostRequestAdd;
import com.zixuan.sina.entity.vo.PostVo;
import com.zixuan.sina.service.PostService;
import com.zixuan.sina.utils.FileUploadUtils;
import com.zixuan.sina.utils.MultipleMapUtil;
import com.zixuan.sina.utils.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/post/")
public class PostController {
    @Resource
    private FileUploadUtils fileUploadUtils;
    @Resource
    private PostService postService;
    @Resource
    private MultipleMapUtil multipleMapUtil;

    @GetMapping("list")
    public Result<List<PostVo>> list() {
        return postService.listPost();
    }

    //管理员获取违规帖子列表
    @GetMapping("getViolationPost")
    public Result<List<PostMangerVo>> getViolationPost(@RequestParam(required = false,name = "keyword") String keyword){
        return postService.getViolationPost(keyword);
    }

    @PostMapping("queryPost")
    public Result<List<PostVo>> queryPost(@RequestBody PostQuery postQuery) {
        return postService.queryPost(postQuery);
    }
    @GetMapping("getPost/{postId}")
    public Result getPost(@PathVariable String postId){
        return postService.getPost(postId);
    }

    //更新post
    @PostMapping("/update")
    public Result update(@RequestBody Post post) {

        return postService.updatePost(post);
    }

    //添加post
    @PostMapping("add")
    public Result add(@RequestBody PostRequestAdd post) {
        return postService.add(post);
    }

    /**
     * //图片/视频上传
     *
     * @param file         上传的文件信息
     * @param type：上传的文件类型
     * @return
     */


//    @Resource
//    private RabbitTemplate rabbitTemplate;
//
//    @Resource
//    private DirectChange directChange;

    @PostMapping("upload")
    public Result upload(MultipartFile file, @RequestParam("type") String type) {
        //todo:调用文件上传功能
        String fileUrl = null;
        try {
            multipleMapUtil.cacheMultiple("file", file);
            fileUrl = fileUploadUtils.chunkUpload();
//            fileUrl = fileUploadUtils.sshFtp(file);
//            rabbitTemplate.convertAndSend("my_change", "my_routingKey", "321313");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Post post = new Post();
        if ("video".equals(type)) {
            post.setVideoUrl(fileUrl);
        } else if ("img".equals(type)) {
            post.setImgUrl(fileUrl);
        }
        return Result.ok(post);
    }

    //post点赞
    @PostMapping("thumbStar")
    public Result thumbStar(@RequestBody List<String> postIds) {

        return postService.thumbStar(postIds);
    }

    //设置精选文章
    @GetMapping("setSpecial")
    public Result setSpecial(@RequestParam("postId") String postId) {
        return postService.setSpecial(postId);
    }
    //文章删除
    @GetMapping("delete")
    public Result deletePost(@RequestParam("postId") String postId){
        return postService.deletePost(postId);
    }
}
